package com.empire.module.member.controller.admin.casepersonrelation;

import com.empire.module.member.controller.admin.casepersonrelation.vo.CasePersonRelationPageReqVO;
import com.empire.module.member.controller.admin.casepersonrelation.vo.CasePersonRelationRespVO;
import com.empire.module.member.controller.admin.casepersonrelation.vo.CasePersonRelationSaveReqVO;
import com.empire.module.member.dal.dataobject.casepersonrelation.CasePersonRelationDO;
import com.empire.module.member.service.casepersonrelation.CasePersonRelationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.util.List;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 案件-人员关系")
@RestController
@RequestMapping("/member/case-person-relation")
@Validated
public class CasePersonRelationController {

    @Resource
    private CasePersonRelationService casePersonRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建案件-人员关系")
    @PreAuthorize("@ss.hasPermission('person:case-person-relation:create')")
    public CommonResult<Integer> createCasePersonRelation(@Valid @RequestBody CasePersonRelationSaveReqVO createReqVO) {
        return success(casePersonRelationService.createCasePersonRelation(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案件-人员关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('person:case-person-relation:delete')")
    public CommonResult<Boolean> deleteCasePersonRelation(@RequestParam("caseId") String caseId, @RequestParam("personId") Long personId) {
        casePersonRelationService.deleteCasePersonRelation(caseId, personId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案件-人员关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('person:case-person-relation:query')")
    public CommonResult<CasePersonRelationRespVO> getCasePersonRelation(@RequestParam("id") Long id) {
        CasePersonRelationDO casePersonRelation = casePersonRelationService.getCasePersonRelation(id);
        return success(BeanUtils.toBean(casePersonRelation, CasePersonRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案件-人员关系分页")
    @PreAuthorize("@ss.hasPermission('person:case-person-relation:query')")
    public CommonResult<PageResult<CasePersonRelationRespVO>> getCasePersonRelationPage(@Valid CasePersonRelationPageReqVO pageReqVO) {
        PageResult<CasePersonRelationDO> pageResult = casePersonRelationService.getCasePersonRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CasePersonRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案件-人员关系 Excel")
    @PreAuthorize("@ss.hasPermission('person:case-person-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCasePersonRelationExcel(@Valid CasePersonRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CasePersonRelationDO> list = casePersonRelationService.getCasePersonRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "案件-人员关系.xls", "数据", CasePersonRelationRespVO.class,
                        BeanUtils.toBean(list, CasePersonRelationRespVO.class));
    }

}